package com.sale.system.controller;

import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.domain.SysMaterialCoding;
import com.sale.system.domain.dto.MaterialRawDto;
import com.sale.system.domain.vo.MaterialRawVo;
import com.sale.system.service.IMaterialRawService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 原料信息
 * @Param
 * @Date 23-07-13
 * @Author hl
 **/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/raw")
public class MaterialRawController extends BaseController {

    private final IMaterialRawService materialRawService;

    /**
     * 获取原料配置
     */
    //@SaCheckPermission("system:Ingredients:list")
    @GetMapping("/list")
    public TableDataInfo<MaterialRawDto> list(MaterialRawVo materialRaw, PageQuery pageQuery) {
        return materialRawService.selectRawList(materialRaw,pageQuery);
    }

    /**
     * 新增原料信息
     */
    //@SaCheckPermission("system:Ingredients:add")
    @Log(title = "原料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody MaterialRawVo materialRaw) throws Exception {
        return toAjax(materialRawService.insertRaw(materialRaw));
    }

    /**
     * 删除原料信息
     */
    //@SaCheckPermission("system:Ingredients:remove")
    @Log(title = "原料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialId}")
    public R<Void> remove(@PathVariable("materialId") String materialId) throws Exception {
        return toAjax(materialRawService.deleteRawById(materialId));
    }

    /**
     * 根据编号获取详细信息
     */
    @GetMapping(value = "/{materialId}")
    //@SaCheckPermission("system:Ingredients:query")
    public R<Map<String, Object>> getInfo(@PathVariable long materialId)
    {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("detailed", materialRawService.selectRawById(materialId));
        return R.ok(ajax);
    }

    /**
     * 修改原料信息
     */
    //@SaCheckPermission("system:Ingredients:edit")
    @Log(title = "原料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody MaterialRawVo materialRaw) throws Exception {
        return toAjax(materialRawService.updateRawEdit(materialRaw));
    }

    /**
     * 树结构
     *
     * @param
     * @return 原料集合
     */
    @GetMapping("/listChildren")
    public R<Map<String, Object>> listChildren()
    {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("theDrop", materialRawService.selectChildrenRawThe());
        return R.ok(ajax);
    }

    /**
     * 根据物料编码获取原料详情
     * @param materialCode
     * @return
     */
    @GetMapping("/getByMaterialCode/{materialCode}")
    public MaterialRawDto getByMaterialCode(@PathVariable String materialCode)
    {
        return materialRawService.getByMaterialCode(materialCode);
    }

    /**
     * 根据原料类别查询类别名称
     */
    @GetMapping("/selectCodingRulesById")
    public R<SysMaterialCoding> selectCodingRulesById(String codingId) {
        return R.ok(materialRawService.selectCodingRulesById(codingId));
    }

    /**
     * 原料冻结
     */
    //@RequiresPermissions("material:awmateria:editFreeze")
    @Log(title = "原料信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editFreeze/{materialId}")
    public R editFreeze(@PathVariable Long materialId) {
        return R.ok(materialRawService.updateEditFreeze(materialId));
    }

    /**
     * 原料解冻
     */
   // @RequiresPermissions("material:awmateria:editThaw")
    @PutMapping("/editThaw/{materialId}")
    public R editThaw(@PathVariable Long materialId) {
        return R.ok(materialRawService.updateEditThaw(materialId));
    }

}
